<?php
namespace app\manage\model;
use think\Db;
use think\Model;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/5
 * Time: 16:25
 */
class Payment extends Model
{
    public function cs(){

    }

    /**
     * 租房显示
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function index(){
        $data = input();
        $where=[];
        $page=isset($data['page']) ? trim($data['page']) : 1;
        $pay_state=isset($data['state']) ? trim($data['state']) : '';
        $type=isset($data['type']) ? trim($data['type']) : '';
        $number=isset($data['number']) ? trim($data['number']) : '';
        $a_time=isset($data['time']) ? strtotime(trim($data['time'])) : '';
        $where['a.create_id'] = create_id($data['token']);
        if($type!=null){
            $where['a.c_pay'] = array('eq',$type);
        }
        //退租
        if($pay_state==2){
            if($number!=null){
                if($number!=null){
                    $where['a.c_number|c.tenant_name'] = ['like',"%".$number."%"];
                    //$where['b.t_type'] = array('eq',1);
                }
            }
            if($a_time!=null){
                $add_time=$a_time;//开始时间
                $e_time=$a_time+1*24*60*60 ;//结束时间
                $where['rent_time'] = array('between', array($add_time,$e_time));
            }
            $where['a.c_state'] = array('in','4,6');
        }else{
            if($number!=null){
                if($number!=null){
                    $where['a.c_number|d.name'] = ['like',"%".$number."%"];
                }
            }
            if($a_time!=null){
                $add_time=$a_time;//开始时间
                $e_time=$a_time+1*24*60*60 ;//结束时间
                $where['f.a_time'] = array('between', array($add_time,$e_time));
            }
            $where['a.c_state'] = array('in','1,2,3,5');
        }
        $count=Db::name('contract_management')
            ->alias('a')
            ->join('tenant_contract b','a.c_id=b.t_c_mid')
            ->join('tenant_management c','b.t_id=c.tenant_id')
            ->join('admin_user d','c.tenant_agent=d.admin_id')
            ->join('contracted_housing e','a.c_id=e.c_c_mid')
            ->join('payment f','a.c_id=f.c_id')
            ->where(['b.t_type'=>1])
            ->where($where)
            ->group('a.c_id')
            ->count();
        $data=Db::name('contract_management')
            ->alias('a')
            ->join('tenant_contract b','a.c_id=b.t_c_mid')
            ->join('tenant_management c','b.t_id=c.tenant_id')
            ->join('admin_user d','c.tenant_agent=d.admin_id')
            ->join('contracted_housing e','a.c_id=e.c_c_mid')
            ->join('payment f','a.c_id=f.c_id')
            ->field('a.c_id,a.c_a_time,a.c_d_time,a.c_pay,a.rent_time,a.c_room,a.c_number,a.c_time,a.c_rent,c.tenant_name,c.tenant_tel,d.name,d.admin_tel,a.c_signing_time,f.*')
            ->where(['b.t_type'=>1])
            ->where($where)
            ->page($page, 10)
            ->order("a.c_id desc,f.pay_time desc")
            ->group('a.c_id')
            ->select();
        foreach($data as $k=>$v){
            $data[$k]['payment']=Db::name('payment')
                ->field('pay_time,pay_type,pay_state')
                ->where(['c_id'=>$v['c_id']])
                ->order('pay_id desc')
                ->find();
            if($pay_state==2){
                $data[$k]['housing']=Db::name('contracted_housing')
                    ->where(['c_c_mid'=>$v['c_id']])
                    ->select();
                $data[$k]['rent']=Db::name('rent_withdrawal')
                    ->where(['c_id'=>$v['c_id']])
                    ->find();
            }
        }

        //   var_dump($count,$data);exit;
        $date['data']=$data;
        $date['count']=$count;
        return $date;
    }

    /**
     * 租房回显
     * @param $id 合同id
     * @return bool|int|string
     */
    public function PayOne($id){
        $data=Db::name('contract_management')->where(['c_id'=>$id])->find();//合同显示
        $data['c_img']=explode(',',$data['c_img']);
        $data['c_method']=json_decode($data['c_method']);
        $pay=Db::name('payment')->where(['c_id'=>$id])->select();//合同缴费记录
        foreach($pay as $k=>$v){
            $pay[$k]['pay_img']=explode(',',$v['pay_img']);
        }

        $data['payment']=$pay;
        $tenant= Db::name('tenant_contract')->alias('a')
            ->join('tenant_management b','a.t_id=b.tenant_id')
            ->where(['t_c_mid'=>$id])
            ->field('a.t_c_id,b.tenant_name,b.tenant_tel,b.tenant_idcard')
            ->where(['t_type'=>2])
            ->select();//合同合租者
        $data['tenant']=$tenant;
        $contr= Db::name('tenant_contract')->alias('a')
            ->join('tenant_management b','a.t_id=b.tenant_id')
            ->field('a.t_c_id,b.tenant_name,b.tenant_tel,b.tenant_idcard')
            ->where(['t_c_mid'=>$id])
            ->where(['t_type'=>1])
            ->find();//合同租客
        $data['contract']=$contr;
        $housing=Db::name('contracted_housing')->where(['c_c_mid'=>$id])->select();//合同房源
        $data['housing']=$housing;
        return $data;
    }

    /**
     * 确认收款
     * @param $id 缴费id
     * @param $c_id 合同id
     * @return bool|int|string
     */
    public function Confirm($id,$c_id){
        Db::startTrans();
        try {
            Db::name('payment')->where(['pay_id'=>$id])->update(['pay_state'=>1]);//合同显示
            Db::name('contract_management')->where(['c_id'=>$c_id])->update(['c_pay'=>1]);//合同显示
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            return false;
        }
    }
    /**
     * 退租确认收款
     * @param $c_id 合同id
     * @return bool|int|string
     */
    public function rentWithdrawal($c_id,$type){
        Db::startTrans();
        try {
            if($type==1){
                Db::name('contract_management')->where(['c_id'=>$c_id])->update(['c_pay'=>1]);//合同显示
            }else{
                Db::name('contract_management')->where(['c_id'=>$c_id])->update(['c_pay'=>2]);//合同显示
            }
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            return false;
        }
    }

    /**
     * 退房回显
     * @param $id 合同id
     * @return bool|int|string
     */
    public function PayRentWithdrawal($id){
        $data=Db::name('contract_management')->where(['c_id'=>$id])->find();//合同显示
        $data['c_img']=explode(',',$data['c_img']);
        $data['c_method']=json_decode($data['c_method']);
        $pay=Db::name('payment')->where(['c_id'=>$id])->select();//合同缴费记录
        foreach($pay as $k=>$v){
            $pay[$k]['pay_img']=explode(',',$v['pay_img']);
        }
        $data['payment']=$pay;
        $tenant= Db::name('tenant_contract')->alias('a')
            ->join('tenant_management b','a.t_id=b.tenant_id')
            ->where(['t_c_mid'=>$id])
            ->field('a.t_c_id,b.tenant_name,b.tenant_tel,b.tenant_idcard')
            ->where(['t_type'=>2])
            ->select();//合同合租者
        $data['tenant']=$tenant;
        $contr= Db::name('tenant_contract')->alias('a')
            ->join('tenant_management b','a.t_id=b.tenant_id')
            ->field('a.t_c_id,b.tenant_name,b.tenant_tel,b.tenant_idcard')
            ->where(['t_c_mid'=>$id])
            ->where(['t_type'=>1])
            ->find();//合同租客
        $data['contract']=$contr;

        $rent=Db::name('rent_withdrawal')->where(['c_id'=>$id])->find();//合同退租处理情况
        $count=Db::name('contracted_housing')->where(['c_c_mid'=>$id])->count();//合同房源数量
        $housing=[];
        if($count>1){
            $housing=Db::name('contracted_housing')->where(['c_c_mid'=>$id])->select();//合同房源
        }else{
            $housing=Db::name('contracted_housing')->where(['c_c_mid'=>$id])->find();//合同房源
            $rent=Db::name('rent_withdrawal')->where(['c_id'=>$id])->find();//合同退租处理情况
            $items=Db::name('handover_items')->where(['c_h_id'=>$housing['c_h_id']])->find();//房源物品
            $user=Db::name('resident_users')->where(['resident_agent'=>$housing['c_h_id']])->find();//合同房源入住人
            $data['user']=$user;
            $data['items']=$items;
        }
        $data['rent']=$rent;
        $data['housing']=$housing;
        $data['count']=$count;
     //   var_dump($data);exit;
        return $data;
    }


}